/**
 * Created by lijian on 2017/3/1.
 */


angular.module('app').service("uploadService",['Upload', '$uibModal', function (Upload, $uibModal) {
    this.openUploadDialog = function (cb) {
        var modalInstance = $uibModal.open({
            templateUrl: 'tpl/common/upload.html',
            controller: ["$scope", function ($scope) {

                $scope.data = {
                    url: null,
                    file: null,
                    defaultImage: "img/default-thumbnail.png"
                };
                $scope.url = "";

                $scope.selectTabIndex = 0;

                $scope.tabChange = function (index) {
                    $scope.selectTabIndex = index;
                };

                $scope.upload = function () {
                    if (!$scope.data.file) {
                        return;
                    }

                    var data = {file: $scope.data.file};
                    Upload.upload({
                        url: serverPath("common/upload"),
                        data: data
                    }).success(function (result) {
                        if (result.successful) {
                            modalInstance.close(result.data);
                        }
                    });
                };

                $scope.onSubmit = function (valid) {
                    if ($scope.selectTabIndex == 0) {
                        $scope.upload();
                    } else {
                        if (valid) {
                            modalInstance.close($scope.data.url);
                        } else {
                            modalInstance.close();
                        }
                    }
                };

                $scope.cancel = function () {
                    modalInstance.close();
                };
            }],
            // size: size,
            resolve: {
                account: function () {
                    return {};
                }
            }
        });

        modalInstance.result.then(function (result) {
            if (result) {
                if (cb) {
                    cb(result);
                }
            }
        }, function () {
        });

        return modalInstance;
    }

    this.openUploadFileDialog = function (cb) {
        var modalInstance = $uibModal.open({
            templateUrl: 'tpl/common/upload_file.html',
            controller: ["$scope", function ($scope) {

                $scope.data = {
                    url: null,
                    file: null
                };
                $scope.url = "";
                $scope.upload = function () {
                    if (!$scope.data.file) {
                        return;
                    }

                    var data = {file: $scope.data.file};
                    Upload.upload({
                        url: serverPath("common/upload"),
                        data: data
                    }).success(function (result) {
                        if (result.successful) {
                            var data={fileName:$scope.data.file.name,url:result.data};
                            modalInstance.close(data);
                        }
                    });
                };

                $scope.onSubmit = function (valid) {
                    $scope.upload();
                };

                $scope.cancel = function () {
                    modalInstance.close();
                };
            }],
            // size: size,
            resolve: {
                account: function () {
                    return {};
                }
            }
        });

        modalInstance.result.then(function (result) {
            if (result) {
                if (cb) {
                    cb(result);
                }
            }
        }, function () {
        });

        return modalInstance;
    }

}]);